<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2023/2/8
 * Time: 上午10:46
 */

namespace app\common\lib;

use think\queue\Job;
use think\Log;

/**
 * 账号模拟
 *  通过修改.env配置文件中的内容实现
 * Class Simulation
 * @package app\common\lib
 */
class Simulation
{
    /**
     * 账号模拟处理
     * @param Job $job
     * @param $jobData
     *  type int 0=>关闭模拟,1=>开启模拟
     *  uid int 需要模拟的账号ID
     *  aid int 管理员ID
     *  name string 管理员名
     */
    public function deal(Job $job, $jobData)
    {
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 1)
        {
            $job->delete();
        }
        $host = Util::getEcsIp();
        // 账号模拟开启/关闭 1=>开启
        $type = isset($jobData['type']) ? (int)$jobData['type'] : 0;

        if ($type)
        {
            $logInfo = '账号模拟开启';
        } else
        {
            $logInfo = '账号模拟关闭';
        }
        $logInfo .= ": host:{$host} uid:{$jobData['uid']} admin_id:{$jobData['aid']} admin_name:{$jobData['name']}";

        try
        {
            $path = ROOT_PATH;

            if ($type)
            {
                // 第6行为账号模拟的配置
                exec("sed -i '6s/.*/to={$jobData['uid']}/g' {$path}.env");
            } else
            {
                // 第6行为账号模拟的配置
                exec("sed -i '6s/.*/;to=2/g' {$path}.env");
            }
            $logInfo .= ' success';
        }
        catch (\Exception $e)
        {
            $logInfo .= ' error:' . $e->getMessage() . ' [' . $e->getFile() . ':' . $e->getLine() . ']';
        }

        $logInfo .= ' jobData:' . json_encode($jobData, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        print(date('Y-m-d H:i:s') . ' ' . $logInfo);

        Log::write($logInfo, 'log', true);

        $job->delete();
    }

    /**
     * 准有好事账号模拟处理
     * @param Job $job
     * @param $jobData
     *  type int 0=>关闭模拟,1=>开启模拟
     *  uid int 需要模拟的账号ID
     *  aid int 管理员ID
     *  name string 管理员名
     */
    public function zhunle_good_deal(Job $job, $jobData)
    {
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 1)
        {
            $job->delete();
        }
        $host = Util::getEcsIp();
        // 账号模拟开启/关闭 1=>开启
        $type = isset($jobData['type']) ? (int)$jobData['type'] : 0;

        if ($type)
        {
            $logInfo = '账号模拟开启';
        } else
        {
            $logInfo = '账号模拟关闭';
        }
        $logInfo .= ": host:{$host} uid:{$jobData['uid']} admin_id:{$jobData['aid']} admin_name:{$jobData['name']}";

        try
        {
            $path = ROOT_PATH;

            if ($type)
            {
                // 第6行为账号模拟的配置
                exec("sed -i '11s/.*/to={$jobData['uid']}/g' {$path}.env");
            } else
            {
                // 第6行为账号模拟的配置
                exec("sed -i '11s/.*/;to=2/g' {$path}.env");
            }
            $logInfo .= ' success';
        }
        catch (\Exception $e)
        {
            $logInfo .= ' error:' . $e->getMessage() . ' [' . $e->getFile() . ':' . $e->getLine() . ']';
        }

        $logInfo .= ' jobData:' . json_encode($jobData, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        print(date('Y-m-d H:i:s') . ' ' . $logInfo);

        Log::write($logInfo, 'log', true);

        $job->delete();
    }

}


